package svm.coelce.parser.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

public class ConexaoUtil {
	private static Connection conexao;

	private ConexaoUtil(){}

	public static Connection getConnection(){
		if( ConexaoUtil.conexao == null ){
			try {
				Class.forName( "com.microsoft.sqlserver.jdbc.SQLServerDriver" );

			} catch( ClassNotFoundException classNotFoundException ){
				classNotFoundException.printStackTrace();
			}

			try {
				conexao = DriverManager.getConnection( "jdbc:sqlserver://svm0012:1055;databaseName=Gestor;user=gestor;password=g3st0r;" );

			} catch( SQLException sqlException ){
				sqlException.printStackTrace();
			}
		}

		return conexao;
	}

	public static void fecharConexao(){
		try {
			if( ConexaoUtil.conexao != null && !ConexaoUtil.conexao.isClosed() ){
				ConexaoUtil.conexao.close();
				ConexaoUtil.conexao = null;
			}
		} catch( SQLException sqlException ){
			sqlException.printStackTrace();
		}
	}

	public static void closeResultSet( ResultSet resultSet ){
		try {
			if( resultSet != null && !resultSet.isClosed() ){
				resultSet.close();
			}
		} catch( SQLException sqlException ){
			sqlException.printStackTrace();
		}
	}
}
